/*
 * function.c
 *
 *  Created on: May 21, 2012
 *      Author: root
 */

#include "unp.h"
#include "ftp_client.h"

void get_myaddress(int sockfd) {
	struct sockaddr_in localaddr;
	socklen_t addrlen;
	addrlen = (sizeof(localaddr));
	getsockname(sockfd, (struct sockaddr*) &localaddr, &addrlen);
	printf("my local address : %s\n", inet_ntoa(localaddr.sin_addr));
	strcpy(my_addr, inet_ntoa(localaddr.sin_addr));

}




void send_request(int sockfd, char *cmd, char *arg1) {
	if (!strcmp(arg1, "")) {
		sprintf(request, "%s\r\n", cmd);
	} else {
		sprintf(request, "%s %s\r\n", cmd, arg1);
	}

	if(toggle==1)
	{
	printf("------>");
	fflush(stdout);
	fputs(request, stdout);
	}
	Writen(sockfd, request, strlen(request));
	memset(request, 0, strlen(request));
}

char parse_LF(char *arg1) {

	char *token;
	char temp[100];


	token = strtok(arg1, "\n");
	strcpy(temp, token);

	return (strlen(temp));
}

void make_PORT_arg() {
	char argument[30];
	strcpy(argument, my_addr);


	char *cut;
	char addr1[4], addr2[4], addr3[4], addr4[4];
	int port1_i, port2_i;
	char port1_c[10], port2_c[10];
	cut = strtok(argument, ".");
	strcpy(addr1, cut);
	cut = strtok(NULL, ".");
	strcpy(addr2, cut);
	cut = strtok(NULL, ".");
	strcpy(addr3, cut);
	cut = strtok(NULL, ".");
	strcpy(addr4, cut);

	srand((unsigned)time(NULL));
	port1_i = rand() % 250;
	port2_i = rand() % 250;
	int port = port1_i * 256 + port2_i;

	sprintf(active_data_port, "%d", port);




	sprintf(port1_c, "%d", port1_i);
	sprintf(port2_c, "%d", port2_i);

	sprintf(PORT_arg, "%s,%s,%s,%s,%s,%s", addr1, addr2, addr3, addr4, port1_c,
			port2_c);


}

int not_connect(USER_CMD cmd_info)
{
	int not_con;
	not_con=0;
	if(!strcmp(cmd_info.cmd,"user")||!strcmp(cmd_info.cmd,"ls")||!strcmp(cmd_info.cmd,"dir")
	||!strcmp(cmd_info.cmd,"nlist")||!strcmp(cmd_info.cmd,"get")||!strcmp(cmd_info.cmd,"put")
	||!strcmp(cmd_info.cmd,"pwd")||!strcmp(cmd_info.cmd,"cd")||!strcmp(cmd_info.cmd,"ascii")
	||!strcmp(cmd_info.cmd,"binary")||!strcmp(cmd_info.cmd,"system")||!strcmp(cmd_info.cmd,"close")
	||!strcmp(cmd_info.cmd,"bye")||!strcmp(cmd_info.cmd,"quit")||!strcmp(cmd_info.cmd,"type"))
	{
		not_con=1;
		return(not_con);
	}
	else
	{
		return(not_con);
	}
}




